package ij.hkust.gui.working;

import ij.IJ;
import ij.ImagePlus;
import ij.gui.ImageCanvas;
import ij.gui.ImageWindow;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * this window is used to show the 
 * @author liuquan
 *
 */
public class WorkingWin{

	public static int count = 0;
	private static  List<ImageWindow> iw =new ArrayList<ImageWindow>();
	private static Map<Integer,ImageWindow> iws = new HashMap<Integer,ImageWindow>();
	private static List<Integer> channels = new ArrayList<Integer>();
	public WorkingWin(){}
	
	public static void update(int chan,String imgPath){
//		IJ.log("update working win");
		count ++;
		if(count % 10 !=0){
			return;
		}
		if(channels.contains(new Integer(chan))){
			//already have one
			ImagePlus ip = new ImagePlus(imgPath);
			
//			for(int i=0;i<iw.size();i++){
//				if(iw.get(i).getTitle().equals(chan+"")){
//					iw.get(i).setImage(ip);
//					iw.get(i).updateImage(ip);
//					iw.get(i).setTitle(chan+"");
////					IJ.log("update working win:" + chan + "  " + imgPath);
//				}
//			}
			ImageWindow tmpWin = iws.get(new Integer(chan));
			if(tmpWin == null){
				IJ.log("update the windows wrong!!!");
			}
			int width = tmpWin.getImagePlus().getWidth();
			int height = tmpWin.getImagePlus().getHeight();
//			tmpWin.setImage(ip);
			tmpWin.getImagePlus().setImage(ip.getImage());
			tmpWin.repaint();
			tmpWin.setTitle("Channel" + chan+"");
//			tmpWin.setSize(size);
//			tmpWin.getCanvas().setSize(size);
//			IJ.log("canvas Size new:" + tmpWin.getCanvas().getSize().toString());
//			ip.setTitle(chan+"");
			return;
		}else{
			channels.add(chan);
			ImagePlus ip = new ImagePlus(imgPath);
			ImageCanvas ic = new ImageCanvas(ip);
			ImageWindow tmp = new ImageWindow(ip,ic);
			tmp.setImage(ip);
			tmp.setTitle(chan+"");
//			iw.add(tmp);
			iws.put(new Integer(chan), tmp);
		}
		
	}
	public static void update(int chan,ImagePlus imp){
//		IJ.log("update working win");
		count ++;
		if(count % 10 !=0){
			return;
		}
		if(channels.contains(new Integer(chan))){
			//already have one
			//ImagePlus ip = new ImagePlus(imgPath);
			
			for(int i=0;i<iw.size();i++){
				if(iw.get(i).getTitle().equals(chan+"")){
//					iw.get(i).setImage(imp);
					iw.get(i).updateImage(imp);
//					iw.get(i).setTitle(chan+"");
					iw.get(i).setTitle(chan+"");
//					IJ.log("update working win:" + chan + "  " + imgPath);
				}
			}
//			ip.setTitle(chan+"");
			return;
		}else{
			channels.add(chan);
//			ImagePlus ip = new ImagePlus(imgPath);
			ImageCanvas ic = new ImageCanvas(imp);
			ImageWindow tmp = new ImageWindow(imp,ic);
			tmp.setImage(imp);
			tmp.setTitle(chan+"");
			iw.add(tmp);
		}
		
	}

	/**
	 * close all the old windows,this should be called before start a new one, or close the old one.
	 */
	public static void closeAll(){
		for(int i=0;i<iw.size();i++){
			if(iw.get(i).isShowing())
				iw.get(i).close();
		}
		iw.clear();
		channels.clear();
	}
}
